//
//  Personal_VC.m
//  K-Music
//
//  Created by CHUN on 16/1/6.
//  Copyright © 2016年 CHUN. All rights reserved.
//

#import "Personal_VC.h"
#import "WMPageController.h"
#import "Person_D_one.h"
#import "Person_D_two.h"
#import "Person_D_three.h"


@interface Personal_VC ()<UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate>{


}
@property (nonatomic, strong) UITableView *Person_TableView;

@end

@implementation Personal_VC

-(void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated
{
    self.navigationController.navigationBarHidden=NO;
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.navigationItem.title = @"个人中心";
    
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithIcon:@"play" highIcon:@"play" target:self action:@selector(play_play)];
    
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"back" highIcon:@"back" target:self action:@selector(back_back)];
    
    [self createUI];
    
}

- (void)play_play
{
    NSLog(@"play");
    
    //[MBProgressHUD showErrorWithStatus:@"播放器" toView:self.view];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"music_play" object:self];
}

-(void)dealloc{
    
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"music_play" object:nil];
    
}

- (void)back_back
{
    self.tabBarController.tabBar.hidden = NO;
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)createUI
{
    _Person_TableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-20) style:UITableViewStylePlain];
    _Person_TableView.delegate = self;
    _Person_TableView.dataSource = self;
    _Person_TableView.showsVerticalScrollIndicator = NO;
    _Person_TableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _Person_TableView.backgroundColor = RGB(241, 241, 241);
    [self.view addSubview:_Person_TableView];
    
    UIImageView * HeaderView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 100)];
    HeaderView.image = [UIImage imageNamed:@"preson_b"];
    _Person_TableView.tableHeaderView = HeaderView;
    
    HeaderView.userInteractionEnabled = YES;
    
    UIImageView*image=[[UIImageView alloc]initWithFrame:CGRectMake(10, 20, 60,60)];
    image.layer.cornerRadius = image.frame.size.width/2;
    image.layer.masksToBounds = YES;
    [image setUserInteractionEnabled:YES];
    
    NSString * imageurl = [NSString stringWithFormat:@"%@",[FileStorage NSUserDefault_getObjectForKey:user_image]];
    
    if ([imageurl isEqualToString:@"001"]) {
        image.image=[UIImage imageNamed:@"icon_person"];
        
    }
    else
    {
        [image sd_setImageWithURL:[NSURL URLWithString:imageurl]];
    }
    [HeaderView addSubview:image];
    UITapGestureRecognizer * PersonalTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(Personal_D)];
    [HeaderView addGestureRecognizer:PersonalTap];
    
    
    UILabel *name=[[UILabel alloc]initWithFrame:CGRectMake(image.frame.size.width+image.frame.origin.x+20, 20, WIDTH/2, 30)];
    name.font = [UIFont systemFontOfSize:15];
    if (imageurl.length == 0 ) {
        name.text = @"CHUN";
    }
    else
    {
        name.text= [NSString stringWithFormat:@"%@",[FileStorage NSUserDefault_getObjectForKey:nickName]];
        
    }
    
    [HeaderView addSubview:name];
    
    UILabel*info=[[UILabel alloc]initWithFrame:CGRectMake(image.frame.size.width+image.frame.origin.x+20, 50, WIDTH/4, 30)];
    info.font = [UIFont systemFontOfSize:15];
    if (imageurl.length == 0 ) {
        info.text = @"K号：10000";
    }else
    {
        info.text = [NSString stringWithFormat:@"K号：%@",[FileStorage NSUserDefault_getObjectForKey:User_id]];
    }
    
    [HeaderView addSubview:info];
    
    
    UIImageView* VIP =[[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(info.frame), 55,25,15)];
    VIP.image = [UIImage imageNamed:@"vip_one"];
    [HeaderView addSubview:VIP];
    
    UIImageView* star_one =[[UIImageView alloc]initWithFrame:CGRectMake(WIDTH-50, 30,30,25)];
    star_one.image = [UIImage imageNamed:@"star_one"];
//    [HeaderView addSubview:star_one];
    
    UIImageView* star_two =[[UIImageView alloc]initWithFrame:CGRectMake(WIDTH-50, 70,30,25)];
    star_two.image = [UIImage imageNamed:@"star_two"];
//    [HeaderView addSubview:star_two];

}

- (void)Personal_D
{
    NSLog(@"头像");
    NSArray * viewControllers = @[[Person_D_one class], [Person_D_two class],[Person_D_three class]];
    
    NSArray *titles = @[@"作品", @"动态",@"成就"];
    
    WMPageController *pageVC = [[WMPageController alloc] initWithViewControllerClasses:viewControllers andTheirTitles:titles];
    
    pageVC.viewFrame = CGRectMake(0, 100, WIDTH, HEIGHT-100);
    pageVC.pageAnimatable = YES;
    pageVC.menuItemWidth = 100;
    pageVC.menuHeight = 45;
    pageVC.titleSizeSelected = 15;
    pageVC.titleColorSelected = [UIColor orangeColor];
    pageVC.postNotification = YES;
    pageVC.bounces = YES;
    
    // 下划线
    pageVC.menuViewStyle = WMMenuViewStyleLine;
    
    pageVC.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"back" highIcon:@"back" target:self action:@selector(back_back)];
    
    pageVC.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithIcon:@"play" highIcon:@"play" target:self action:@selector(play_play)];
    
    UIImageView * Header_D = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 100)];
//    Header_D.image = [UIImage imageNamed:@"preson_b"];
    Header_D.backgroundColor = RGB(244, 219, 182);
    [pageVC.view addSubview: Header_D];
    
    UIImageView*image=[[UIImageView alloc]initWithFrame:CGRectMake(10, 20, 60 , 60)];
    image.layer.cornerRadius= image.frame.size.width/2;
    image.layer.masksToBounds = YES;
    
    NSString * imageurl = [NSString stringWithFormat:@"%@",[FileStorage NSUserDefault_getObjectForKey:user_image]];
    
    if ([imageurl isEqualToString:@"001"]) {
        image.image=[UIImage imageNamed:@"icon_person"];
    }
    else
    {
        [image sd_setImageWithURL:[NSURL URLWithString:imageurl]];
    }
    [Header_D addSubview:image];
    
    
    UILabel *name=[[UILabel alloc]initWithFrame:CGRectMake(image.frame.size.width+image.frame.origin.x+20, 20, WIDTH/2, 30)];
    name.font = [UIFont systemFontOfSize:15];
    if (imageurl.length == 0 ) {
        name.text = @"CHUN";
    }
    else
    {
        name.text= [NSString stringWithFormat:@"%@",[FileStorage NSUserDefault_getObjectForKey:nickName]];
    }
    
    [Header_D addSubview:name];
    
    UILabel*info=[[UILabel alloc]initWithFrame:CGRectMake(image.frame.size.width+image.frame.origin.x+20, 50, WIDTH/4, 30)];
    info.font = [UIFont systemFontOfSize:15];
    if (imageurl.length == 0 ) {
        info.text = @"K号：10000";
    }else
    {
        info.text = [NSString stringWithFormat:@"K号：%@",[FileStorage NSUserDefault_getObjectForKey:User_id]];
    }

    [Header_D addSubview:info];
    
    UIImageView* VIP =[[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(info.frame), 55,25,15)];
    VIP.image = [UIImage imageNamed:@"vip_one"];
    [Header_D addSubview:VIP];
    
    UIImageView* star_one =[[UIImageView alloc]initWithFrame:CGRectMake(WIDTH-50, 20,30,25)];
    star_one.image = [UIImage imageNamed:@"star_one"];
    [Header_D addSubview:star_one];
    
    UIImageView* star_two =[[UIImageView alloc]initWithFrame:CGRectMake(WIDTH-50, CGRectGetMaxY(star_one.frame)+10,30,25)];
    star_two.image = [UIImage imageNamed:@"star_two"];
    [Header_D addSubview:star_two];

    [self.navigationController pushViewController:pageVC animated:YES];

}

#pragma mark ------- UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 4) {
        return 0;
    }
    return 8.0f;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 2;
    }
    if (section == 1) {
        return 4;
    }
    if (section == 2) {
        return 4;
    }
    if (section == 3) {
        return 3;
    }
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 4;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 45;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"cellID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    UILabel *iconNameLabel;
    UIImageView *iconView;
    
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellID];
        
        // 图片
        iconView = [[UIImageView alloc] initWithFrame:CGRectMake(15, 12.5, 20, 20)];
        [cell.contentView addSubview:iconView];
        
        iconNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 0, 200, cell.contentView.frame.size.height)];
        
        iconNameLabel.font = [UIFont systemFontOfSize:15.0f];
        iconNameLabel.textAlignment = NSTextAlignmentLeft;
        [cell.contentView addSubview:iconNameLabel];
        
        UIView *lineView2 = [[UIView alloc] initWithFrame:CGRectMake(0, cell.contentView.frame.size.height-1, WIDTH, 1)];
        lineView2.backgroundColor = [UIColor clearColor];
        [cell.contentView addSubview:lineView2];
        
        if (indexPath.row != 0) {
            UIView *lineView1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 1)];
            lineView1.backgroundColor = RGB(241, 241, 241);
            [cell.contentView addSubview:lineView1];
        }
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.accessoryType  = UITableViewCellAccessoryDisclosureIndicator;
    
    NSArray *iconName0 = @[@"会员中心",@"最近访客"];
    NSArray *iconImage0 = @[@"person_01",@"person_02"];
    
    
    NSArray *iconName1 = @[@"个人资料",@"我的金币",@"我的收入",@"免费保险柜"];
    NSArray *iconImage1 = @[@"person_03",@"person_04",@"person_05",@"person_06"];
    
    NSArray *iconName2 = @[@"我的群组",@"我关注的歌友",@"我的粉丝",@"拉黑的兔崽子"];
    NSArray *iconImage2 = @[@"person_07",@"person_08",@"person_09",@"person_09"];
    
    NSArray *iconName3 = @[@"我发起的合唱",@"我关注的合唱",@"邀请我的合唱"];
    NSArray *iconImage3 = @[@"person_10",@"person_11",@"person_12"];
    
    if (indexPath.section == 0) {
        
        iconNameLabel.text = iconName0[indexPath.row];
        iconView.image = [UIImage imageNamed:iconImage0[indexPath.row]];
        
    }
    else if (indexPath.section == 1)
    {
        iconNameLabel.text = iconName1[indexPath.row];
        iconView.image = [UIImage imageNamed:iconImage1[indexPath.row]];

    }
    else if (indexPath.section == 2)
    {
        iconNameLabel.text = iconName2[indexPath.row];
        iconView.image = [UIImage imageNamed:iconImage2[indexPath.row]];

    }
    else if (indexPath.section == 3)
    {
        iconNameLabel.text = iconName3[indexPath.row];
        iconView.image = [UIImage imageNamed:iconImage3[indexPath.row]];

    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.section) {
        case 0:
            if (indexPath.row ==0 ) {
                [self.navigationController pushViewController: [members_A0 new ]animated:YES];
           
            }
            if (indexPath.row ==1 ) {
                [self.navigationController pushViewController: [visitors_A1 new ]animated:YES];
    
            }
            break;
            
        case 1:
            if (indexPath.row ==0 ) {
                [self.navigationController pushViewController: [personal_B0 new ]animated:YES];
                
            }
            if (indexPath.row ==1 ) {
                [self.navigationController pushViewController: [coins_B1 new ]animated:YES];
            }
            if (indexPath.row ==2 ) {
                [self.navigationController pushViewController: [income_B2 new ]animated:YES];
            }
            if (indexPath.row ==3 ) {
                [self.navigationController pushViewController: [backpack_B3 new ]animated:YES];
            }
            break;
            
        case 2:
            if (indexPath.row ==0 ) {
                
                [self.navigationController pushViewController: [Group_C0 new ]animated:YES];
                
                //self.tabBarController.selectedIndex = 3;
                //self.tabBarController.tabBar.hidden = NO;
            }
            
            if (indexPath.row ==1 ) {
                [self.navigationController pushViewController: [geyou_C1 new ]animated:YES];
            }
            if (indexPath.row ==2 ) {
                [self.navigationController pushViewController: [fans_C2 new ]animated:YES];
            }
            if (indexPath.row ==3 ) {
                [self.navigationController pushViewController: [Blacklist_C3 new ]animated:YES];
            }
            break;
            
        case 3:
            if (indexPath.row ==0 ) {
                [self.navigationController pushViewController: [me_chorus_D0 new ]animated:YES];
            }
            if (indexPath.row ==1 ) {
                [self.navigationController pushViewController: [attention_chorus_D1 new ]animated:YES];
            }
            if (indexPath.row ==2 ) {
                [self.navigationController pushViewController: [invitation_chorus_D2 new ]animated:YES];
            }
            break;
            
        default:
            break;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
